package com.zoz.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zoz.constant.CommonConstant;
import com.zoz.constant.DatabaseConstant;
import com.zoz.dto.ColumnDto;
import com.zoz.service.impl.DatabaseServiceImpl;

import cn.hutool.core.util.StrUtil;

/**
 * 引擎公共方法
 *@author zhuhg
 *@date 2020年6月5日 下午1:38:48
 */
public class EngineUtil {
	
	private static final Logger logger = LoggerFactory.getLogger(EngineUtil.class);
	/**
	 * 获得接口前缀
	 *@author zhuhg
	 *@date 2020年6月5日 下午1:41:45
	 * 
	 * @param projectName
	 * @return
	 */
	public static String getApiProject(String projectName) {
		String [] apiProjects = projectName.split("-");
		StringBuilder stb = new StringBuilder();
		for (String str : apiProjects) {
			stb.append("/" + str);
		}
		return stb.toString();
	}
	
	/**
	 * 根据数据库列封装java字段
	 *@author zhuhg
	 *@date 2020年6月5日 下午2:08:06
	 * {
            CommonConstant.COLUMN_NAME: "sys_id",
            "column_comment": "系统id",
            "data_type": "int",
            "is_nullable": "NO"
        }
	 * @param columns
	 * @return
	 */
	public static List<ColumnDto> getColumnList(String columns){
		List<JSONObject> columnList = JSON.parseArray(columns, JSONObject.class);
		List<ColumnDto> dataList = new ArrayList<>();
		for (JSONObject json : columnList) {
			ColumnDto column = new ColumnDto();
			column.setColumn(json.getString(CommonConstant.COLUMN_NAME));
			column.setRemark(json.getString("column_comment"));
			column.setColumnType(DatabaseConstant.JDBC_TYPE_MAP.get(json.getString("data_type")));
			column.setField(ToCamelUtil.toCapitalizeCamelCase(json.getString(CommonConstant.COLUMN_NAME)));
			column.setChangeField(ToCamelUtil.toCamelCase(json.getString(CommonConstant.COLUMN_NAME)));
			column.setColumnJavaType(DatabaseConstant.TYPE_MAP.get(json.getString("data_type")));
			column.setIsNull(json.getString("is_nullable"));
			column.setSave(json.getString("save"));
			column.setSelect(json.getString("select"));
			column.setUpdate(json.getString("update"));
			column.setDelete(json.getString("delete"));
			column.setTable(json.getString("table"));
			column.setDefaultVal(StrUtil.isBlank(json.getString("column_default")) ? "" : json.getString("column_default"));
			column.setEl(StrUtil.isBlank(json.getString("el")) ? "" : json.getString("el"));
			String columnName = json.getString(CommonConstant.COLUMN_NAME);
			column.setFormatter(columnName.contains("type") || columnName.contains("status")|| columnName.contains("yn") ? "true":"");
			dataList.add(column);
		}
		return dataList;
	}
	/**
	 * 根据主路径获得模板路径
	 *@author zhuhg
	 *@date 2020年6月5日 下午3:13:01
	 * 
	 * @param templates
	 * @param path
	 * @param className
	 * @param pack
	 * @return
	 */
	public static List<JSONObject> getTemplatePaths (List<String> templates,String className,String pack){
		List<JSONObject> paths = new ArrayList<>();
		for (String pathUrl : templates) {
			String [] names = pathUrl.split("\\\\");
			// 获得文件名称
			String name = names[names.length -1 ].replace(".ftl", "");
			//如果有文件名称包含.xml算xml文件 需要改变路经
			String pathPack = name.contains(".") ? pack.replace("java", "resources") : pack;
			// 转义成包名 有驼峰的算两个路径 去除.xml
			String packe = name.substring(0, 1).toLowerCase() + name.substring(1).replace(".xml", "");
			String pathTo ="";
			//是前端的进行重新封装
			if(packe.contains(".vue")) {
				pathTo = pathPack  + File.separator +className +  File.separator + name;
			}else {
				// 将两个路径拆开
				packe = ToCamelUtil.toLowerCaseUnderScoreCase(packe, false).replace(ToCamelUtil.SEPARATOR, File.separator);
				//
				pathTo = pathPack + File.separator + packe + File.separator + className + name + (name.endsWith(".xml") ?"" : ".java");
			}
			JSONObject json = new JSONObject();
			json.put("java", pathTo);
			json.put("template", pathUrl.replace("\\\\", File.separator));
			paths.add(json);
		}
		return paths;
	}
}
